﻿#region using
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AwesomeTanks.Sources.Media;
using Microsoft.Xna.Framework.Media;
#endregion

namespace AwesomeTanks.Sources.GameElements.HUD
{
    /// <summary>
    /// The timer of a Game of AwesomeTanks
    /// </summary>
    public class GameTimer
    {
        #region Fields
        public static int GAMEOVERPOSITIONTRANSLATIONX = 150;
        public static int NULLTIMEPOSITIONTRANSLATIONX = 50;
        public static int ENDGAME_VOLUME_REDUCTION = 2;

        public TimeSpan? RemainingTime { get; set; }
        public Vector2 Position { get; set; }
        public Boolean GameFinish { get; set; }
        public Sprite Infinite { get; set; }

        private bool countdownPlayed = false;
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the component <see cref="GameTimer"/> class. Infinite Timer in this case.
        /// </summary>
        public GameTimer(Vector2 position)
        {
            this.Position = position;
            RemainingTime = null;
            GameFinish = false;
            Infinite = new Sprite(Art.SpriteSheet, Art.InfiniteSourceRectangle, position);
            Infinite.Origin = Art.InfiniteOrigin;
            Infinite.Color = Color.Tan;
        }
        /// <summary>
        /// Initializes a new instance of the component <see cref="GameTimer"/> class.
        /// </summary>
        /// <param name="position">The position.</param>
        /// <param name="minutes">The minutes.</param>
        /// <param name="seconds">The seconds.</param>
        public GameTimer(Vector2 position, int minutes, int seconds)
        {
            this.Position = position;
            RemainingTime = new TimeSpan(0, minutes, seconds);
            GameFinish = false;
        }
        #endregion

        #region Update and Draw
        /// <summary>
        /// Updates the Timer at the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public void Update(GameTime gameTime)
        {
            if (RemainingTime != null)
            {
                TimeSpan RemainingTimeNotNull = (TimeSpan)RemainingTime;

                if (RemainingTimeNotNull.TotalSeconds > 0)
                {
                    //Substract the Time Elapsed
                    RemainingTime -= gameTime.ElapsedGameTime;
                }

                if (RemainingTimeNotNull.TotalSeconds <= 10 && !countdownPlayed)
                {
                    MediaPlayer.Volume /= ENDGAME_VOLUME_REDUCTION;
                    countdownPlayed = true;
                    Sound.CountDown.Play();
                }

                //Change the position of the Timer if the time is finished
                if (RemainingTimeNotNull.TotalSeconds <= 0 && !GameFinish)
                {
                    GameFinish = true;
                }
            }
        }

        /// <summary>
        /// Draws the Timer with the specified sprite batch and with the specified sprite font.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="spritefont">The spritefont.</param>
        public void Draw(SpriteBatch spriteBatch, SpriteFont spritefont)
        {
            if (RemainingTime != null)
            {
                //Text to write with his color
                String text = "";
                Color textColor = Color.Tan;
                TimeSpan RemainingTimeNotNull = (TimeSpan)RemainingTime;

                //Add a "0" for beautiful visual effect
                if (RemainingTimeNotNull.Minutes > 0)
                {
                    text = String.Format("{0}:{1}{2}", RemainingTimeNotNull.Minutes, (RemainingTimeNotNull.Seconds < 10) ? "0" : "", RemainingTimeNotNull.Seconds);
                }
                else
                {
                    //Time Finish
                    if (RemainingTimeNotNull.TotalSeconds <= 0)
                    {
                        text = "Game Over";
                    }

                    // Change the color for a visual effect of a twinkle
                    else
                    {
                        text = RemainingTimeNotNull.Seconds.ToString();
                        if (RemainingTimeNotNull.Seconds <= 10 && RemainingTimeNotNull.Seconds % 2 == 1)
                        {
                            textColor = Color.Gray;
                        }
                    }
                }

                Vector2 origin = spritefont.MeasureString(text) / 2;
                spriteBatch.DrawString(spritefont, text, this.Position, textColor, 0, origin, 1f, SpriteEffects.None, 0);
            }
            else
            {
                Infinite.Draw(spriteBatch);
            }            
        }
        #endregion
    }
}
